<?php
class User extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->check_isvalidated();
		$this->output->set_header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
		$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
		$this->output->set_header('Cache-Control: post-check=0, pre-check=0',false);
		$this->output->set_header('Pragma: no-cache');
		$this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	}
	public function index()
	{
		$data = get_initial_data($this);
		$data['title'] ='Profile';
		$this->load->view('templates/header',$data);
		$this->load->view('profile',$data);
		$this->load->view('templates/footer',$data);
	}


	public function update(){

		$this->form_validation->set_rules('username', 'username',  'trim|required|xss_clean');
		$this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean|callback_check_database');

		$this->form_validation->set_rules('new_password', 'new password',  'trim|required|xss_clean|min_length[5]|max_length[12]');
		$this->form_validation->set_rules('confirm_password', 'confirm password', 'trim|required|xss_clean|matches[new_password]');

		$data = get_initial_data($this);
		if ($this->form_validation->run()){
			$this->load->model('user_model');
			$this->user_model->update();

			$data['title']='Information';
			$data['message'] = Messages::success_update_profile;
			$this->load->view('templates/header',$data);
			$this->load->view('message',$data);
			$this->load->view('templates/footer',$data);

		}else{
			$data['title']='Profile';
			$this->load->view('templates/header',$data);
			$this->load->view('profile',$data);
			$this->load->view('templates/footer',$data);
		}
	}

	public function check_database(){

		$this->load->model('user_model');

		if($this->user_model->validate()){
			return true;
		}else{
			$this->form_validation->set_message('check_database', '*invalid user name or password');
			return false;
		}
	}

	public function create(){
		$this->form_validation->set_rules('create_username', 'create_username',  'trim|required|xss_clean|callback_check_exist');
		$this->form_validation->set_rules('create_password', 'create_password',  'trim|required|xss_clean|min_length[5]|max_length[12]');
		$this->form_validation->set_rules('create_confirm_password', 'create_confirm_password', 'trim|required|xss_clean|matches[create_password]');
		$data = get_initial_data($this);
		if ($this->form_validation->run()){
			$this->load->model('user_model');
			if($this->user_model->create()){
				$data['message'] = Messages::success_create_profile;
			}else{
				$data['message'] = Messages::success_update_profile;
			}
			$data['title']='Information';
			$this->load->view('templates/header',$data);
			$this->load->view('message',$data);
			$this->load->view('templates/footer',$data);
		}else{
			$data['title'] ='Profile';
			$this->load->view('templates/header',$data);
			$this->load->view('profile',$data);
			$this->load->view('templates/footer',$data);
		}
	}
	
	public function check_exist(){
	
		$this->load->model('user_model');
		$username = $this->security->xss_clean($this->input->post('create_username'));
	
		if($this->user_model->user_name_exist($username)){
			$this->form_validation->set_message('check_exist', '*user name is unavailable');
			return false;
		}else{
			return true;
		}
	}

	private function check_isvalidated(){
		if(!$this->session->userdata('validated')){
			redirect('');
		}
	}

}